package com.blb.edufileservice.controller;

import com.blb.common.util.ResponseResult;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 文件控制器
 */
@Slf4j
@RestController
public class FileController {

    @Autowired
    private FastFileStorageClient fastClient;

    @Value("${fdfs.ip}")
    private String ip;

    @PostMapping("/file-upload")
    public ResponseResult<String> fileUpload(MultipartFile file) throws IOException {
        //截取文件后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传到fastdfs 参数1 文件流 参数2 文件大小 参数3 后缀名 参数4 配置参数
        StorePath storePath = fastClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        log.info("文件保存成功：{}",storePath.getFullPath());
        return ResponseResult.ok("http://" + ip + "/" + storePath.getFullPath());
    }
}
